package com.lyw.springCloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lyw.springCloud.dao.StorageDao;
import com.lyw.springCloud.domain.Order;
import com.lyw.springCloud.domain.Storage;
import com.lyw.springCloud.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Auther liyawei
 * @Date 2022/5/21 11:14
 */

@Slf4j
@Service
public class StorageServiceImpl implements StorageService {

    @Resource
    private StorageDao storageDao;

    @Override
    public void decrease(Order order) {
        log.info("-------->storage-service中扣减库存");

        LambdaUpdateWrapper<Storage> wrapper = new LambdaUpdateWrapper<>();
        wrapper.ge(Storage::getProductId, order.getProductId());
        Storage storage = storageDao.selectOne(wrapper);
        storage.setResidue(storage.getResidue() - order.getCount());
        storage.setUsed(storage.getUsed() + order.getCount());
        storageDao.update(storage, new UpdateWrapper<Storage>().lambda().ge(Storage::getProductId, order.getProductId()));

    }
}
